﻿using System.Collections.Generic;
using System.Linq;
using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class AdminMenuPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IAdminMenuView _view;
        
        public AdminMenuPresenter(IAdminMenuView view)
            : this(view, new RepositoryFactory())
        {
        }
        
        public AdminMenuPresenter(IAdminMenuView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
        }

        public void Initialize()
        {
            IList<VisitedAdminUrl> adminUrls = new List<VisitedAdminUrl>();
            for (int i = _administrator.VisitedAdminUrls.Count - 1; i >= 0; i--)
            {
                VisitedAdminUrl adminUrl = _administrator.VisitedAdminUrls[i];
                if (adminUrl.CurrentSite != _administrator.CurrentSite)
                    continue;
                adminUrls.Add(_administrator.VisitedAdminUrls[i]);
                if (adminUrls.Count >= 20)
                    break;
            }
            _view.SetLatestVisitedAdminUrls(adminUrls);

            IList<VisitedPublicUrl> publicUrls = new List<VisitedPublicUrl>();
            for (int i = _administrator.VisitedPublicUrls.Count - 1; i >= 0; i--)
            {
                VisitedPublicUrl publicUrl = _administrator.VisitedPublicUrls[i];
                if (publicUrl.Page.Site != _administrator.CurrentSite)
                    continue;
                publicUrls.Add(publicUrl);
                if (publicUrls.Count >= 20)
                    break;
            }
            _view.SetLatestVisitedPublicUrls(publicUrls);
            var customAdminPages = SystemRepository.GetCustomAdminPages(_administrator.CurrentSite.ID).Where(cap => cap.ShowInMenu).ToList();
            if (_administrator.Group.ID != 1)
            {
                customAdminPages = customAdminPages.Where(cap => 
                                            (
                                                _administrator.Group.DefaultCustomAdminPageAccess == DefaultAccess.Allow && 
                                                !cap.DeniedAdministratorGroups.Contains(_administrator.Group)
                                            ) ||
                                            (
                                                _administrator.Group.DefaultCustomAdminPageAccess == DefaultAccess.Deny && 
                                                cap.AllowedAdministratorGroups.Contains(_administrator.Group)
                                            )
                                        ).ToList();
            }
            _view.SetCustomAdminPages(customAdminPages);
        }
        
        public Administrator GetAdministrator()
        {
            return _administrator;
        }
    }
}